package com.jiesen.base.modules.login.jsLoginUser.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiesen.base.baseDao.RedisServiceImpl;
import com.jiesen.base.modules.login.jsLoginUser.model.JsLoginUser;
import com.jiesen.base.modules.login.jsLoginUser.model.UserLogin;
import com.jiesen.base.modules.login.jsLoginUser.service.JsLoginUserService;
import com.jiesen.base.modules.login.jsLoginUser.utils.JsLoginUserUtil;
import com.jiesen.base.modules.login.jsRole.model.JsRole;
import com.jiesen.base.modules.login.jsRole.service.JsRoleService;
import com.jiesen.base.utils.JWTUtil;
import com.jiesen.base.utils.PasswordUtils;
import com.jiesen.base.utils.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/login/user")
public class JsLoginUserController {

    @Autowired
    public JsLoginUserService jsLoginUserService;

    @Autowired
    private JsRoleService jsRoleService;

    //根据用户id或者token查询后台登录用户
    @GetMapping("/view")
    public R viewUser(HttpServletRequest request,@RequestParam(value = "id",required = false)String id) {
        String token = request.getHeader("token");
        if(id==null)
        id = JWTUtil.getUserId(token);

        JsLoginUser jsLoginUser = jsLoginUserService.getById(id);
        if (jsLoginUser == null) {
            return R.error("没有用户");
        }
        jsLoginUser.setPassword("");
        return R.ok().put("data", jsLoginUser);
    }

    //根据条件查询用户信息
    @PostMapping("/view")
    //@RequiresPermissions("jsuser:list")
    public R viewAll(HttpServletRequest request,@RequestBody JsLoginUser jsLoginUser){
        IPage<JsLoginUser> jsLoginUserIPage = jsLoginUserService.page1(jsLoginUser);
        List<JsLoginUser> records = jsLoginUserIPage.getRecords();
        for (JsLoginUser record : records) {
            Integer id = record.getId();
            record.setPassword(null);
            List<JsRole> roles = jsRoleService.findRoleByUserId(id);
            record.setJsRoles(roles);
        }
        return R.ok().put("data",records).put("total",jsLoginUserIPage.getTotal());
    }

    //增加一个后台登录用户
    @PostMapping("/create")
    public R creacrUser(@RequestBody JsLoginUser jsLoginUser) {
        //手机号没有，直接返回
        if (jsLoginUser.getTelephone() == null) {
            return R.error("没有手机号");
        }
        //没有姓名,直接返回
        if(jsLoginUser.getName()==null){
            return R.error("没有姓名");
        }
        //完善用户信息
        jsLoginUser = JsLoginUserUtil.newJsLoginUser(jsLoginUser);

        boolean save = jsLoginUserService.save(jsLoginUser);
        if (!save) {
            return R.error("添加失败！");
        }
        return R.ok();
    }

    //删除一个后台登录用户
    @GetMapping("/delete")
    //@RequiresPermissions("jsuser:del")
    public R deleteUser(@RequestParam("id") String id) {
        boolean save = jsLoginUserService.removeById(id);
        if (!save) {
            return R.error("没有用户");
        }
        return R.ok();
    }

    //修改一个后台用户
    @PostMapping("/edit")
    //@RequiresPermissions("jsuser:add")
    public R deitUser(@RequestBody JsLoginUser jsLoginUser) {
        jsLoginUser.setUts(new Date());
        jsLoginUser.setPassword(null);
        String password = jsLoginUser.getPassword();
        if(password!=null||!password.equals("")){
            String passwordSecurity = PasswordUtils.getPasswordSecurity(password);
            jsLoginUser.setPassword(passwordSecurity);
        }
        boolean save = jsLoginUserService.updateById(jsLoginUser);
        if (!save) {
            return R.error("没有用户");
        }
        return R.ok();
    }

    //查询用户拥有的角色
    @GetMapping("/role/view")
    //@RequiresPermissions("jsuser:list")
    public R roleView(@RequestParam("id") Integer id) {
        JsLoginUser user = jsLoginUserService.findRoleByUserId(id);
        if (user == null) {
            return R.error("没有用户");
        }
        return R.ok().put("data",user);
    }

    //给用户修改角色
    @PostMapping("/role/add")
    //@RequiresPermissions("jsuser:add")
    public R roleAdd(@RequestBody JsLoginUser jsLoginUser) {
        List<Integer> rid = jsLoginUser.getRids();
        boolean i = jsLoginUserService.updataUserRole(jsLoginUser.getId(), rid);
        if (!i) {
            return R.error();
        }
        return R.ok();
    }

    @GetMapping("/selectsssssssssssssssss")
    @RequiresRoles(logical = Logical.OR, value = {"管理员", "捷森教育"})
    public R select(@RequestParam("id") String id) {
        JsLoginUser jsLoginUser = jsLoginUserService.getById(id);
        if (jsLoginUser == null) {
            return R.error("用户不存在");
        }
        return R.ok().put("data", jsLoginUser);
    }

}
